package jalgebrava.group.groups;

import jalgebrava.group.Group;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public final class QuaternionGroupFactory {
	private final static Map<Integer, Group<Integer>> cache = new HashMap<Integer, Group<Integer>>();
	final static Group<Integer> mk(int n) {
		if (n!=8) {
			throw new RuntimeException("Quaternion group for " + n);
		}
		
		// look in cache
		Group<Integer> result;
		result = cache.get(n);
		if (result != null) {
			return result;
		}
		result = mkWorker(n);
		cache.put(n, result);
		return result; 
	}
	
	private final static Group<Integer> mkWorker(int n) {
		final List<String> elements = new ArrayList<String>(8) {{
			this.add("1"); this.add("-1");
			this.add("i"); this.add("-i");
			this.add("j"); this.add("-j");
			this.add("k"); this.add("-k");
		}};
		int p1 = 0;
		int m1 = 1;
		int pi = 2;
		int mi = 3;
		int pj = 4;
		int mj = 5;
		int pk = 6;
		int mk = 7;
		int table[][] = {
				{p1,m1,pi,mi,pj,mj,pk,mk},
				{m1,p1,mi,pi,mj,pj,mk,pk},
				{pi,mi,m1,p1,mk,pk,pj,mj},
				{mi,pi,p1,m1,pk,mk,mj,pj},
				{pj,mj,pk,mk,m1,p1,mi,pi},
				{mj,pj,mk,pk,p1,m1,pi,mi},
				{pk,mk,mj,pj,pi,mi,m1,p1},
				{mk,pk,pj,mj,mi,pi,p1,m1},
		};
		
		return BlackBoxGroupFactory.mk("Q"+n, table, elements.toArray(new String[8]));
	}
}
